package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;
import sun.security.krb5.Config;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class PageantClearLog extends BaseRepair {

    public PageantClearLog(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void goLogin(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLogLogin("2020-11-03","2020-11-13");
        DataMgr.addData(listMap);
    }

    public void go1(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1("2020-11-03","2020-11-13");
        DataMgr.addData(listMap);
    }
    public void go2(){
        List<Map<String, String>> listMap = new ArrayList<>();
        String activityIdIn = "9006005,9006109,9006278,9006432,9006535,9006635,9006735,9006874,9006971,9007069,9007225";
        listMap = getLog2(activityIdIn);
        DataMgr.addData(listMap);
    }

    public void go3(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog3();
        DataMgr.addData(listMap);
    }

    public void goIntegral(){
        List<Map<String, String>> listMap = new ArrayList<>();
        String activityIdIn = "9006005,9006109,9006278,9006432,9006535,9006635,9006735,9006874,9006971,9007069,9007225";
        listMap = getLogIntegral(activityIdIn);
        listMap = getMaxIntegral(listMap);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLogLogin(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId " +
                " from t_l_login " +
                " where `time` >= ? AND `time` <= ?" +
                " GROUP BY userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                UserInfo userInfo = getUserInfo(rs.getString("userId"));
                temp.put("vipExp",String.valueOf(userInfo.getVipExp()));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLog1(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_buy " +
                " where shopType = ? AND `time` >= ? AND `time` <= ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,"12");
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("mallId",checkNullToEmpty(rs.getString("mallId")));
                temp.put("price",checkNullToEmpty(rs.getString("price")));
                temp.put("count",checkNullToEmpty(rs.getString("count")));
                temp.put("time",checkNullToEmpty(rs.getString("time")));
                UserInfo userInfo = getUserInfo(rs.getString("userId"));
                temp.put("sex",checkNullToEmpty(String.valueOf(userInfo.getSex())));
                temp.put("vipExp",checkNullToEmpty(String.valueOf(userInfo.getVipExp())));

                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
    private List<Map<String, String>> getLog2(String activityIdIn) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_pageant_clear_log where activityId in("+activityIdIn+")";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("clearLevel",checkNullToEmpty(rs.getString("clearLevel")));
                temp.put("clearTime",checkNullToEmpty(rs.getString("clearTime")));
                UserInfo userInfo = getUserInfo(rs.getString("userId"));
                temp.put("sex",checkNullToEmpty(String.valueOf(userInfo.getSex())));
                temp.put("vipExp",checkNullToEmpty(String.valueOf(userInfo.getVipExp())));

                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
    private List<Map<String, String>> getLog3() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_goods " +
                " WHERE way = 390004";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("goodsId",checkNullToEmpty(rs.getString("goodsId")));
                temp.put("isGet",checkNullToEmpty(rs.getString("isGet")));
                temp.put("num",checkNullToEmpty(rs.getString("num")));
                temp.put("nowNum",checkNullToEmpty(rs.getString("nowNum")));
                temp.put("way",checkNullToEmpty(rs.getString("way")));
                temp.put("time",checkNullToEmpty(rs.getString("time")));
                UserInfo userInfo = getUserInfo(rs.getString("userId"));
                temp.put("sex",checkNullToEmpty(String.valueOf(userInfo.getSex())));
                temp.put("vipExp",checkNullToEmpty(String.valueOf(userInfo.getVipExp())));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLogIntegral(String activityIdIn) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,activityId,`value` " +
                " from t_u_activity_condition " +
                " WHERE activityId in ("+activityIdIn+") AND `type` = 79";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("总积分",checkNullToEmpty(rs.getString("value")));
                UserInfo userInfo = getUserInfo(rs.getString("userId"));
                temp.put("sex",checkNullToEmpty(String.valueOf(userInfo.getSex())));
                temp.put("vipExp",checkNullToEmpty(String.valueOf(userInfo.getVipExp())));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getMaxIntegral(List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,activityId,max(`value`+0) as maxOne " +
                " FROM t_u_activity_condition " +
                " WHERE activityId = ? AND userId = ? AND `type` = 78";
        try {
            for(Map<String,String> data : idataList){
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1,data.get("activityId"));
                call.setString(2,data.get("userId"));
                rs = call.executeQuery();
                if(rs.next()){
                    data.put("单局最大",checkNullToEmpty(rs.getString("maxOne")));
                }else{
                    data.put("单局最大","");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }
}
